import Mock from 'mockjs'

function param2Obj(url) {
    const search = url.split('?')[1];
    if (!search) {
        return {}
    }

    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
        '"}'
    )
}

export default {
    getMenu: config => {
        const { username, password } = param2Obj(config.url)
            //先判断用户是否存在
            // 判断账号密码是否对应
        if (username === 'admin' && password === 'admin') {
            return {
                code: 20000,
                data: {
                    menu: [{
                            path: '/home',
                            name: 'home',
                            label: "首页",
                            icon: "s-home",
                            url: "home"
                        },
                        {
                            path: '/mall',
                            name: 'mall',
                            label: "商品管理",
                            icon: "video-play",
                            url: "mall"
                        },
                        {
                            path: '/user',
                            name: 'user',
                            label: "用户管理",
                            icon: "user",
                            url: "user"
                        },
                        {
                            label: "其它",
                            path: '/location',
                            icon: 'location',
                            children: [{
                                    path: "/page1",
                                    name: 'page1',
                                    label: '页面1',
                                    icon: 'setting',
                                    url: "other/pageOne"
                                },
                                {
                                    path: '/page2',
                                    name: 'page2',
                                    label: '页面2',
                                    icon: 'setting',
                                    url: 'other/pageTwo'
                                }
                            ]
                        }
                    ],
                    token: Mock.Random.guid(),
                    message: '获取成功'
                },
            }
        } else if (username === 'admin2' && password === 'admin2') {
            return {
                code: 20000,
                data: {
                    menu: [{
                            path: '/',
                            name: 'home',
                            label: "首页",
                            icon: "s-home",
                            url: "home"
                        },
                        {
                            path: '/mall',
                            name: 'mall',
                            label: "商品管理",
                            icon: "video-play",
                            url: "mall"
                        },
                    ],
                    token: Mock.Random.guid(),
                    message: '获取成功'
                }
            }
        } else {
            return {
                code: 999,
                data: {
                    message: '账号或密码输入错误'
                }
            }
        }
    }
}